<template>
  <qx-table :dbRowClick="rowDblClick" :option="tableOption" :remoteData="remoteData">
    <template v-slot:top>
      <qx-form v-model="searchForm" :option="tableOption" isSearch @submit="startSearch"></qx-form>
    </template>
    <template v-slot:menuLeft>
      <q-btn outline text-color="primary" @click="handleSync">
        {{ $t('Misc.SynchronousHeadquarters') }}
      </q-btn>
    </template>
    <template v-slot:menuLeftR>
      <q-icon color="warning" name="mdi-map-marker-question cursor-pointer cursor-hover" size="1.5rem">
        <q-tooltip class="q-pa-md" v-bind="$tooltipAttr">{{ $t('Misc.DblClickCardTips') }}</q-tooltip>
      </q-icon>
    </template>
    <template v-slot:menu="{ row }">
      <q-btn :disable="row.sdaiType !== '10'" class="col" color="primary" outline unelevated
             @click="handleExtParams(row)">
        {{ $t('RCSCore.ReentryBaseURL') }}
      </q-btn>
      <q-btn v-if="row.isEnable === '0'" class="col" color="primary" outline unelevated
             @click="handleIsEnable(row, '1')">
        {{ $t('Enable') }}
      </q-btn>
      <q-btn v-if="row.isEnable === '1'" class="col" color="primary" outline unelevated
             @click="handleIsEnable(row, '0')">
        {{ $t('Disable') }}
      </q-btn>
    </template>
  </qx-table>
  <q-modal v-model="modalVisible" :header="$t('RCSCore.ReentryBaseURL')" :width="500" btnAlign="center" position="right"
           @beforeClose="beforeClose" @confirm="submitForm">
    <qx-form ref="FormRef" v-model="formInfo" :option="tableOption"></qx-form>
  </q-modal>
  <q-modal v-model="infoVisible" :header="`${$t('Misc.SpecifyDetails')}`" :menuBtn="false" :persistent="false"
           width="620">
    <QInfo :columns="tableOption.column || []" :rowInfo="formInfo" cols="2" headerWidth="100">
      <template v-slot:handleExtraParam="{ row }">
        <QLink classes="multiRow_2 text-caption non-selectable extra-params"
               @click="handleJsonPopup(row.handleExtraParam, '.extra-params')">
          {{ row.handleExtraParam }}
        </QLink>
      </template>
      <template v-slot:testExtraParam="{ row }">
        <QLink classes="multiRow_2 text-caption non-selectable test-extra-params"
               @click="handleJsonPopup(row.testExtraParam, '.test-extra-params')">
          {{ row.testExtraParam }}
        </QLink>
      </template>
    </QInfo>
  </q-modal>
  <JsonPopup v-model="popupVisible" :jsonInfo="jsonInfo" :targetEl="targetEl"></JsonPopup>
</template>
<script setup>
import { i18n, toI18nName } from 'boot/i18n.js'
import { postAPI, putAPI } from 'src/api/index.js'
import { getRemoteData, setRemoteData } from 'src/enum/ajaxRemoteData.js'
import { enableOrDisable } from 'src/enum/index.js'
import { notificationObjects, notificationTargetTypes } from 'src/views/TaskCore/enum.js'
import { computed, onMounted, ref } from 'vue'
import { createDialog } from 'src/utils/utils.js'

const formInfo = ref({})
const FormRef = ref(null)
const jsonInfo = ref('')
const AGVBrands = ref([])
const searchForm = ref({})
const targetEl = ref(false)
const popupVisible = ref(false)
const infoVisible = ref(false)
const modalVisible = ref(false)
const remoteData = ref({ dicUrl: '/yfagv/api/sdaiAgvTaskCallback/list' })
getRemoteData('agv_brand', '/yfagv/api/agvCfgBrand/list').then(data => (AGVBrands.value = data))
const isREST = computed(() => formInfo.value.sdaiType === '10')
const isPlugIn = computed(() => formInfo.value.sdaiType === '20')
const tableOption = computed(() => {
  return {
    grid: true, span: 22, export: true, labelWidth: 100, searchSpan: 4, headerWidth: 85, readonly: true,
    searchLabelWidth: 60,
    column: [{
      label: `${i18n.t('RCSCore.InterfaceCode')}`, field: 'sdaiCode', minWidth: 70
    }, {
      label: `${i18n.t('RCSCore.InterfaceName')}`, field: toI18nName('sdaiNameZhCn'),
      minWidth: 70, search: false
    }, {
      label: `${i18n.t('RCSCore.InterfaceCategory')}`, field: 'sdaiType', type: 'select', minWidth: 70,
      options: notificationTargetTypes.value
    }, {
      label: `${i18n.t('TaskCore.NotificationTargetType')}`, field: 'callbackTargetType', type: 'select', minWidth: 70,
      options: notificationObjects.value
    }, {
      label: `${i18n.t('RCSCore.BaseURL')}`, field: 'restBaseUrl', minWidth: 70, search: false, hide: true,
      display: isREST.value, showInfo: isREST.value, readonly: false
    }, {
      label: `${i18n.t('RCSCore.BizURL')}`, field: 'restHandlePath', minWidth: 70, search: false, hide: true,
      display: false, showInfo: isREST.value
    }, {
      label: `${i18n.t('RCSCore.TestURL')}`, field: 'restTestPath', minWidth: 70, search: false, hide: true,
      display: false, showInfo: isREST.value
    }, {
      label: `${i18n.t('RCSCore.PlugInURL')}`, field: 'jarUrl', minWidth: 70, search: false, hide: true,
      display: false, showInfo: isPlugIn.value
    }, {
      label: `${i18n.t('RCSCore.PlugInClassName')}`, field: 'jarClassName', minWidth: 70, hide: true,
      search: false, display: false, showInfo: isPlugIn.value
    }, {
      label: `${i18n.t('RCSCore.PlugInServiceName')}`, field: 'jarServerName', minWidth: 70, hide: true,
      search: false, display: false, showInfo: isPlugIn.value
    }, {
      label: `${i18n.t('RCSCore.BizExtParams')}`, field: 'handleExtraParam', type: 'textarea', minWidth: 70,
      search: false, hide: true, display: false, slot: true
    }, {
      label: `${i18n.t('RCSCore.TestExtParams')}`, field: 'testExtraParam', type: 'textarea', minWidth: 70,
      search: false, hide: true, display: false, slot: true
    }, {
      label: `${i18n.t('IsEnable')}`, field: 'isEnable', type: 'radio', searchType: 'select', value: '1',
      options: enableOrDisable.value, useTag: true, topRight: true, display: false
    }, {
      label: `${i18n.t('Misc.SynchronousTime')}`, field: 'createdTime', display: false, minWidth: 80, search: false
    }, {
      label: `${i18n.t('Remarks')}`, field: 'remark', type: 'textarea', minWidth: 100, search: false, hide: true
    }]
  }
})

const handleJsonPopup = (json, domClass) => {
  jsonInfo.value = json
  if (targetEl.value === domClass) return popupVisible.value = !popupVisible.value
  popupVisible.value = true
  targetEl.value = domClass
}
const handleSync = () => {
  createDialog({ msg: `${i18n.t('Misc.SyncTip3')}` }, () => {
    postAPI('/yfagv/api/syncAgvCfg/syncSdaiAgvTaskCallback', {}, () => startSearch('clear'))
  })
}
const rowDblClick = row => {
  formInfo.value = row
  infoVisible.value = true
}
const handleExtParams = row => {
  formInfo.value = { ...row }
  modalVisible.value = true
}
const handleIsEnable = (row = {}, isEnable) => {
  const msg = isEnable === '1' ? `${i18n.t('EnableTips')}` : `${i18n.t('DisableTips')}`, idList = [row.id]
  createDialog({ msg }, () => {
    putAPI('/yfagv/api/sdaiAgvTaskCallback/enableOrDisable', { idList, isEnable }, () => startSearch('clear'))
  }, () => {})
}
const formatSearchForm = () => ({ ...searchForm.value })
const startSearch = (action, done) => {
  remoteData.value.params = formatSearchForm()
  remoteData.value.clearSelected = true
  if (done) done()
  action === 'clear' && setRemoteData('traffic_ctrl_api')
}
const beforeClose = () => {
  if (FormRef.value) FormRef.value.onReset()
  modalVisible.value = false
}
const submitForm = mDone => {
  FormRef.value.validate((form, done) => {
    postAPI('/yfagv/api/sdaiAgvTaskCallback/saveHandJson', form, () => {
      startSearch()
      done()
      mDone && mDone()
    }, () => {
      done()
      mDone && mDone(false)
    })
  }, () => mDone && mDone(false))
}
onMounted(startSearch)
</script>